package com.testes.umov.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.testes.umov.R;
import com.testes.umov.model.Item;
import com.testes.umov.model.RgbColor;
import com.testes.umov.screens.ItemAdapter;
import com.testes.umov.util.ActivityListener;

public class TemplateActivity extends Activity {

	private ListView listView;
	private LinearLayout header, footer;
	private TextView viewTitle;
	private Button button1, button2;
	private int buttonIconsColor;
	private RgbColor headerColor, footerColor, backgroundColor, buttonsColor, textColor, otherTextColor;
	private ActivityListener listener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_template);		
		header = (LinearLayout)findViewById(R.id.templateHeader);
		
		headerColor = new RgbColor(getIntent().getIntExtra("headerRed", 0),getIntent().getIntExtra("headerGreen",0), getIntent().getIntExtra("headerBlue", 0), ""); 
		footer = (LinearLayout)findViewById(R.id.templateFooter);
		footerColor = new RgbColor(getIntent().getIntExtra("footerRed", 0),getIntent().getIntExtra("footerGreen",0),getIntent().getIntExtra("footerBlue",0), "");		
		backgroundColor = new RgbColor(getIntent().getIntExtra("backgroundRed", 0),getIntent().getIntExtra("backgroundGreen", 0),getIntent().getIntExtra("backgroundBlue",0), "");		
		textColor = new RgbColor(getIntent().getIntExtra("textRed", 0), getIntent().getIntExtra("textGreen", 0),getIntent().getIntExtra("textBlue", 0), "");				
		listView = (ListView)findViewById(R.id.templateListView);		
		viewTitle = (TextView)findViewById(R.id.templateHeaderLabel);
		button1 = (Button)findViewById(R.id.templateButton1);
		button2 = (Button)findViewById(R.id.templateButton2);
		buttonIconsColor = getIntent().getIntExtra("iconColor", 0);
		//listener = (ActivityListener) getIntent().getSerializableExtra("listener");
		setWhiteLabel();
		whaitAndDie();
	}

	public void whaitAndDie() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
					runOnUiThread(new Runnable() {
						public void run() {
							end();
						}
					});
				}
				
			}
		}).start();
	}
	
	private void end() {
		this.finish();
		//this.listener.onActivityFinished();
	}
	
	private void setWhiteLabel() {
		listView.setAdapter(createItemArrayAdapter());
		listView.setBackgroundColor(Color.rgb(backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue()));
		header.setBackgroundColor(Color.rgb(headerColor.getRed(), headerColor.getGreen(), headerColor.getBlue()));
		footer.setBackgroundColor(Color.rgb(footerColor.getRed(), footerColor.getGreen(), footerColor.getBlue()));
		viewTitle.setText("Template Title");
		viewTitle.setTextColor(Color.rgb(textColor.getRed(), textColor.getGreen(), textColor.getBlue()));
		button1.setBackgroundColor(Color.rgb(headerColor.getRed(), headerColor.getGreen(), headerColor.getBlue()));
		button1.setTextColor(Color.rgb(textColor.getRed(), textColor.getGreen(), textColor.getBlue()));
		button2.setBackgroundColor(Color.rgb(headerColor.getRed(), headerColor.getGreen(), headerColor.getBlue()));
		button2.setTextColor(Color.rgb(textColor.getRed(), textColor.getGreen(), textColor.getBlue()));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.layout.activity_template, menu);
		return true;		
	}
	
	private ItemAdapter createItemArrayAdapter() {
		
		List<Item> list = new ArrayList<Item>();
		
		list.add(new Item("Item 1 description 1"));
		list.add(new Item("Item 2 description 2"));
		list.add(new Item("Item 3 description 3"));
		list.add(new Item("Item 4 description 4"));
		list.add(new Item("Item 5 description 5"));
		list.add(new Item("Item 6 description 6"));
		list.add(new Item("Item 7 description 7"));
		list.add(new Item("Item 8 description 8"));
		list.add(new Item("Item 9 description 9"));
		list.add(new Item("Item 10 description 10"));
		list.add(new Item("Item 11 description 11"));
		list.add(new Item("Item 12 description 12"));
		list.add(new Item("Item 13 description 13"));
		list.add(new Item("Item 14 description 14"));
		list.add(new Item("Item 15 description 15"));
		list.add(new Item("Item 16 description 16"));
		list.add(new Item("Item 17 description 17"));
		list.add(new Item("Item 18 description 18"));
		list.add(new Item("Item 19 description 19"));
		list.add(new Item("Item 20 description 20"));
		
		Item [] items = list.toArray(new Item[list.size()]);
		
		ItemAdapter adapter = new ItemAdapter(this, 0, items, buttonIconsColor, Color.rgb(backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue()), Color.rgb(textColor.getRed(), textColor.getGreen(), textColor.getBlue()));
		
		return adapter;
	}
}
